Smiley face

Huang et al. Algorithm_MLCS(Dynamic programming, 2008)


Main features
Abstract of the paper [Huang 2008]

The longest common subsequence and sequence alignment problems have been studied extensively and they can be regarded as the relationship measurement between sequences. However, most of them treat sequences evenly or consider only two sequences. Recently, with the rise of whole-genome duplication research, the doubly conserved synteny relationship among three sequences should be considered. It is a brand new model to find a merging way for understanding the interleaving relationship of sequences. Here, we define the merged LCS problem for measuring the interleaving relationship among three sequences. An O (n3 ) algorithm is first proposed for solving the problem, where n is the sequence length. We further discuss the variant version of this problem with the block information. For the blocked merged LCS problem, we propose an algorithm with time complexity O (n2 m), where m is the number of blocks. An improved O (n2 + nm2 ) algorithm is further proposed for the same blocked problem.

C++ source code
int MLCS_DP(vector< int>T, vector< int>A, vector< int>B, int Tlen, int Alen, int Blen)
{
    vector< vector< int>> AT(Alen + 1, vector< int>(Tlen + 1));
    vector< vector< int>> BT(Blen + 1, vector< int>(Tlen + 1));
    vector< vector< int>> ABT1(Alen + 1, vector< int>(Blen + 1)); 
    vector< vector< int>> ABT2(Alen + 1, vector< int>(Blen + 1));

    AT[0][0] = 0;
    for (int i = 1; i <= Alen; i++)
        AT[i][0] = 0;
    for (int i = 1; i <= Tlen; i++)
        AT[0][i] = 0;
    for (int i = 1; i <= Alen; i++)
        for (int j = 1; j <= Tlen; j++)
        {
            if (A[i] == T[j])
                AT[i][j] = AT[i - 1][j - 1] + 1;
            else
                AT[i][j] = max(AT[i][j - 1], AT[i - 1][j]);
        }
    BT[0][0] = 0;
    for (int i = 1; i <= Blen; i++)
        BT[i][0] = 0;
    for (int i = 1; i <= Tlen; i++)
        BT[0][i] = 0;
    for (int i = 1; i <= Blen; i++)
        for (int j = 1; j <= Tlen; j++)
        {
            if (B[i] == T[j])
                BT[i][j] = BT[i - 1][j - 1] + 1;
            else
                BT[i][j] = max(BT[i][j - 1], BT[i - 1][j]);
        }

    int layer = 1;
    while (1)
    {
        if (layer % 2 == 1) {
            for (int i = 1; i <= Alen; i++) {
                ABT1[i][0] = AT[i][layer];
                for (int j = 1; j <= Blen; j++) {
                    ABT1[0][j] = BT[j][layer];
                    ABT1[i][j] = max(max(ABT1[i - 1][j], ABT1[i][j - 1]), ABT2[i][j]);
                    if (A[i] == T[layer])
                    {
                        ABT1[i][j] = max(ABT1[i][j], ABT2[i - 1][j] + 1);

                    }
                    if (B[j] == T[layer])
                    {
                        ABT1[i][j] = max(ABT1[i][j], ABT2[i][j - 1] + 1);
                    }
                }
            }
            if (layer == Tlen)
                return ABT1[Alen][Blen];
            ++layer;
        }
        else {
            for (int i = 1; i <= Alen; i++) {
                ABT2[i][0] = AT[i][layer];
                for (int j = 1; j <= Blen; j++) {
                    ABT2[0][j] = BT[j][layer];
                    ABT2[i][j] = max(max(ABT2[i - 1][j], ABT2[i][j - 1]), ABT1[i][j]);
                    if (A[i] == T[layer])
                    {
                        ABT2[i][j] = max(ABT2[i][j], ABT1[i - 1][j] + 1);

                    }
                    if (B[j] == T[layer])
                    {
                        ABT2[i][j] = max(ABT2[i][j], ABT1[i][j - 1] + 1);
                    }
                }
            }
            if (layer == Tlen)
                return ABT2[Alen][Blen];
            ++layer;
        }
    }
}

The files
  All_MLCS.cpp

Reference
Huang, K.-S., Yang, C.-B., Tseng, K.-T., Ann, H.-Y., Peng, Y.-H., 2008. Efficient algorithms for finding interleaving relationship between sequences. Information Processing Letters 105, 188–193.